package com.ht.module.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.constant.DicConstant;
import com.ht.module.bus.entity.BusRoomRecord;
import com.ht.module.bus.mapper.BusRoomRecordMapper;
import com.ht.module.bus.service.BusRoomRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.sys.entity.SysUser;
import com.ht.module.sys.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 房间流水记录表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-12-30
 */
@Service
public class BusRoomRecordServiceImpl extends ServiceImpl<BusRoomRecordMapper, BusRoomRecord> implements BusRoomRecordService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<BusRoomRecord> findRoomRecord(BusRoomRecord en) {
        QueryWrapper<BusRoomRecord> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(en.getRoomId())) {
            qw.eq("room_id", en.getRoomId());
        }
        qw.orderByDesc("create_date");
        //获取数据
        List<BusRoomRecord> list = baseMapper.selectList(qw);
        List<String> userIds = new ArrayList<>();
        for (BusRoomRecord record : list) {
            userIds.add(record.getActionUser());
            userIds.add(record.getPassivityUser());
        }
        //定义用户集合
        List<SysUser> sysUsers = new ArrayList<>();
        //查询出当前参与的所有主动人和被动人
        if (!userIds.isEmpty()) {
            sysUsers = sysUserMapper.selectList(
                    new QueryWrapper<SysUser>()
                            .in("id", userIds)
            );
        }
        for (BusRoomRecord record : list) {
            //获取当前动作人
            SysUser actionUser = sysUsers.stream().filter(x -> StrUtil.equals(x.getId(), record.getActionUser())).findFirst().orElse(null);
            if (actionUser != null) {
                record.setActionUserName(actionUser.getName());
                record.setActionUserAvatar(actionUser.getAvatar());
            }
            //获取当前被动人
            SysUser passivityUser = sysUsers.stream().filter(x -> StrUtil.equals(x.getId(), record.getPassivityUser())).findFirst().orElse(null);
            if (passivityUser != null) {
                record.setPassivityUserName(passivityUser.getName());
                record.setPassivityUserAvatar(passivityUser.getAvatar());
            }
            //构建描述
            StringBuffer str = new StringBuffer("<span style='color:#62e90e'>" + actionUser.getName() + "</span>");
            //判断动作类型
            str.append(DicConstant.ActionType.ActionMap.get(record.getActionType()));
            //判断是否存在被动用户，如果不存在-则操作的是积分池
            str.append(passivityUser == null ? "<span style='color:#62e90e'>积分池</span>" : "<span style='color:#62e90e'>" + passivityUser.getName() + "</span>");
            str.append(record.getScore());
            str.append("分");
            if (BeanUtil.isNotEmpty(record.getTaiwanFee()) && record.getTaiwanFee().compareTo(new BigDecimal(0)) > 0) {
                str.append(",抽取台费");
                str.append(record.getTaiwanFee());
                str.append("分,");
                str.append("实际得分:");
                str.append(record.getScore().subtract(record.getTaiwanFee()));
                str.append("分");
            }
            record.setDesc(str.toString());
        }
        return list;
    }
}
